package cc.vv.btpartyjob.partymanager.view

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.inter.LookBigPicFileInterface
import cc.vv.lkbasecomponent.util.LKScreenUtil
import cc.vv.lkimagecomponent2.LKImage
import cc.vv.lkimagecomponent2.lib.mode.ScaleMode
import java.util.*

/**
 * 图片显示自定义view
 * Created by Chen.qi on 2018/10/9.
 */
class PicFileShowView : LinearLayout {
    /**
     * 上下文
     */
    private var mContext: Context? = null

    /**
     * 父布局
     */
    private var llSelectedAll: LinearLayout? = null

    /**
     * 查看大图
     */
    private var largerImageOnClick = LargerImageOnClick()


    private var selectedImageInter: LookBigPicFileInterface? = null

    fun getSelectedImageInter(): LookBigPicFileInterface? {
        return selectedImageInter
    }

    fun setSelectedImageInter(selectedImageInter: LookBigPicFileInterface) {
        this.selectedImageInter = selectedImageInter
    }

    /**
     * 数据集
     */
    private var dataList = ArrayList<String>()

    constructor(context: Context) : this(context, null) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mContext = context
        val inflateView = View.inflate(context, R.layout.layout_selectd_file_view, null)
        llSelectedAll = inflateView.findViewById(R.id.ll_asj_selectedAll)
        this.addView(inflateView)
    }


    /**
     * 填充选项数据
     *
     * @param optionList 数据集
     * @param itemSize   每行有几个选项
     */
    fun addAllSelectedFileView(optionList: ArrayList<String>?, itemSize: Int) {
        if (null == optionList) {
            return
        }
        //清除
        clearAllJobOptionView()
        dataList.clear()
        dataList.addAll(optionList)
        val length = dataList.size
        val sum = length / itemSize
        val add = if (length % itemSize == 0) 0 else 1
        val totalLinLayout = sum + add

        val viewWight = (LKScreenUtil.getScreenWidth() - LKScreenUtil.dp2px(32f)) / itemSize
        val layoutParams1 = LayoutParams(viewWight, LKScreenUtil.dp2px(87f))

        for (j in 0 until totalLinLayout) {
            val honLinearLayout = LinearLayout(context)
            honLinearLayout.setPadding(0, LKScreenUtil.dp2px(10f), 0, 0)
            val iNumber = j * itemSize
            var count = 0
            for (i in iNumber until dataList.size) {
                count++
                if (count == itemSize + 1) {
                    break
                }
                val view = LayoutInflater.from(mContext).inflate(R.layout.layout_selected_file_item_cun, null)
                view.layoutParams = layoutParams1
                val img = view.findViewById<ImageView>(R.id.iv_selected_fileImg_cun)
                val layoutParams = img.layoutParams as RelativeLayout.LayoutParams
                layoutParams.width = viewWight - 10
                layoutParams.height = LKScreenUtil.dp2px(87f)
                img.layoutParams = layoutParams
                val jobOptionObj = dataList[i]
                view.id = i
                view.setOnClickListener(largerImageOnClick)
                LKImage.load().load(jobOptionObj).scale(ScaleMode.CENTER_CROP)
                        .placeHolder(R.mipmap.icon_def_image)
                        .error(R.mipmap.icon_def_image).into(img)

                honLinearLayout.addView(view)
                val tv = TextView(mContext)
                val layoutParamstv = LayoutParams(LKScreenUtil.dp2px(3f), ViewGroup.LayoutParams.WRAP_CONTENT)
                tv.layoutParams = layoutParamstv
                honLinearLayout.addView(tv)
            }
            llSelectedAll?.addView(honLinearLayout)
        }
    }


    /**
     * 清除view
     */
    private fun clearAllJobOptionView() {
        llSelectedAll?.removeAllViews()
    }


    /**
     * 查看大图
     */
    private inner class LargerImageOnClick : OnClickListener {
        override fun onClick(v: View) {
            val position = v.id
            val path = dataList[position]
            if (TextUtils.isEmpty(path)) {
                return
            }
            getSelectedImageInter()?.lookBigImage(position, 0)
        }
    }
}